/**
 * Utility package
 */
package com.sb.money.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Common functions used cross wide.
 * 
 * @author stefan
 * 
 */
public final class CommonFunctions {

	private static final Logger logger = LoggerFactory
			.getLogger(CommonFunctions.class);

	/**
	 * Private constructor.
	 */
	private CommonFunctions() {
	}

	/**
	 * get days as String for the current month concatenated with ','
	 */
	public static String getMonthDaysAsString() {
		if (logger.isTraceEnabled()) {
			logger.trace("BEGIN");
		}

		String result = null;

		Calendar calendar = Calendar.getInstance();

		int minDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
		int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

		for (int i = minDay; i <= maxDay; i++) {
			calendar.set(Calendar.DAY_OF_MONTH, i);

			if (result == null) {
				result = "";
			}

			result += getDateAsString(calendar.getTime(),
					CommonConstants.DATE_FORMAT);

			// last index shouldnt have the delimiter
			if (i != maxDay) {
				result += ",";
			}
		}

		if (logger.isTraceEnabled()) {
			logger.trace("END result=" + result);
		}
		return result;
	}
	
	public static String bla() {
		if (logger.isTraceEnabled()) {
			logger.trace("BEGIN");
		}
	
		String result = "bla";

		if (logger.isTraceEnabled()) {
			logger.trace("END");
		}

		return result;
	}

	/**
	 * @param date
	 *            to format
	 * @param format
	 *            string
	 * @return converted Date to String
	 */
	public static String getDateAsString(final Date date, final String format) {
		if (logger.isTraceEnabled()) {
			logger.trace("BEGIN date=" + date + ", format=" + format);
		}

		String result = null;

		SimpleDateFormat formater = new SimpleDateFormat(format);
		result = formater.format(date);

		if (logger.isTraceEnabled()) {
			logger.trace("END date=" + date + ", format=" + format
					+ ", result=" + result);
		}

		return result;
	}
}